using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XProject.Entity;
using XProject.DAO;
using XProject.Util;
namespace XProject.GUI
{
    public partial class EmailManager : Form
    {
        Contact contact = null;
        EmailDAO daoEmail = new EmailDAO();
        public EmailManager()
        {
            InitializeComponent();
        }
        #region properties
        public Contact Contact
        {
            get { return contact; }
            set { contact = value; }
        }
        #endregion
        private void EmailManager_Load(object sender, EventArgs e)
        {
            if (this.Contact != null)
            {
                foreach (Email email in Contact.Emails)
                {
                    ListItem lsiEmail=new ListItem(email.EmailAddress,email);
                    lsbEmails.Items.Add(lsiEmail);
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Email email = new Email();
            email.EmailAddress = txtEmail.Text;
            if (this.Contact.AddEmail(email) > 0)
            {
                MessageBox.Show("An email has added successfully");
                ListItem lsiEmail = new ListItem(email.EmailAddress, email);
                lsbEmails.Items.Add(lsiEmail);
            }
            else MessageBox.Show("Failed");

        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (lsbEmails.SelectedItem == null)
            {
                MessageBox.Show("Please select an item");
                return;
            }
            ListItem lsiEmail = lsbEmails.SelectedItem as ListItem;
            Email email = lsiEmail.Tag as Email;
            email.EmailAddress = txtEmail.Text;
            if (daoEmail.Update(email) > 0)
            {
                MessageBox.Show("Updated successfully");
            }
            else MessageBox.Show("Cannot update");
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lsbEmails.SelectedItem == null)
            {
                MessageBox.Show("Please select an item");
                return;
            }
            if (MessageBox.Show("Are you sure want to delete the selected email?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.Yes) return;
            ListItem lsiEmail = lsbEmails.SelectedItem as ListItem;
            Email email = lsiEmail.Tag as Email;
            email.EmailAddress = txtEmail.Text;
            if (this.Contact.RemoveEmail(email) > 0)
            {
                MessageBox.Show("Deleted successfully");
                lsbEmails.Items.Remove(lsbEmails.SelectedItem);
            }
            else MessageBox.Show("Cannot delete");
        }

        private void lsbEmails_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lsbEmails.SelectedIndex > -1)
            {
                ListItem lsiEmail = lsbEmails.SelectedItem as ListItem;
                Email email = lsiEmail.Tag as Email;
                txtEmail.Text = email.EmailAddress;
            }
        }
    }
}